package puj.promobil.prometer.adapters;

import java.util.ArrayList;
import java.util.List;

import puj.promobil.prometer.R;
import puj.promobil.prometer.dto.Producto;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ItemProductoAdapter extends BaseAdapter {
	protected Activity activity;
	protected ArrayList<Producto> items;

	public ItemProductoAdapter(Activity activity, ArrayList<Producto> items) {
		this.activity = activity;
		this.items = items;
	}

	public ItemProductoAdapter(Activity activity, List<Producto> items) {
		this.activity = activity;

		this.items = new ArrayList<Producto>();

		for (Producto val : items) {
			this.items.add(val);
		}
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return items.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View vi = convertView;

		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) activity
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			vi = inflater.inflate(R.layout.producto_item, null);
		}

		Producto item = items.get(position);

		TextView id_producto = (TextView) vi.findViewById(R.id.id_producto);
		id_producto.setText(item.getGtin());

		TextView producto = (TextView) vi.findViewById(R.id.producto);
		producto.setText(item.getDesclarga());
		
		ImageView icon=(ImageView)vi.findViewById(R.id.icon);
		
		try {
			
			if(item.getImagen()!=null){
				Bitmap bitmap = BitmapFactory.decodeByteArray(item.getImagen(),0,item.getImagen().length);
				icon.setImageBitmap(bitmap);
				icon.buildDrawingCache();
			}
			
			
					
					
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		

		return vi;
	}

	public void clean() {

		items.clear();
		this.notifyDataSetChanged();
	}

	public void setItems(List<Producto> items) {
		this.items = new ArrayList<Producto>();

		for (Producto val : items) {
			this.items.add(val);
			
		}
		this.notifyDataSetChanged();
	}
}
